package com.qf.admin.service.impl;

import com.qf.admin.dao.AddressDao;
import com.qf.admin.dao.impl.AddressDaoImpl;
import com.qf.admin.domain.AddAddress;
import com.qf.admin.domain.Address;
import com.qf.admin.domain.Orders;
import com.qf.admin.service.AddressService;
import com.qf.commons.TableData;
import com.qf.utils.DbUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AddressServiceImpl implements AddressService {

    private AddressDao addressDao = new AddressDaoImpl();

    @Override
    public List<Address> getAddressByUserId(Integer userId) {
        return addressDao.getAddressByUserId(userId);
    }

    /**
     * @param userId
     * @param addressId  被设置为收货地址的id
     */
    @Override
    public void changeTakeDeliveryAddress(Integer userId, Integer addressId) {
        addressDao.changeTakeDeliveryAddress(userId, addressId);
    }

    @Override
    public void addAddress(Integer userId, String addressName, String phone, String detail, Integer state) {
        //事务手动提交
        Connection conn = DbUtils.getConnection();
        try {
            conn.setAutoCommit(false);  //事务要手动提交
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (state == 1) {
                addressDao.changeState(userId,conn);
            }
            addressDao.addAddress(userId, addressName, phone, detail, state, conn);
            //提交事务
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public void updateAddress(Integer userId, String addressName, String phone, String detail, Integer addressId) {

        //事务手动提交
        Connection conn = DbUtils.getConnection();
        try {
            conn.setAutoCommit(false);  //事务要手动提交
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            addressDao.updateAddress(userId, addressName, phone, detail, addressId, conn);
            //提交事务
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public void deleteAddress(Integer userId, Integer addressId) {
        addressDao.deleteAddress(userId, addressId);
    }

    @Override
    public Address getAddressByAid(Integer aid) {
        return addressDao.getAddressByAid(aid);
    }

    @Override
    public TableData<Orders> getAddress(String name, String goods, Integer status, Integer number, Date beginDate, Date endDate, Integer limit, Integer offset) {
        TableData<Orders> address = addressDao.getAddress(name,goods,status,number,beginDate,endDate,limit,offset);
        return address;
    }

    ////改变订单的状态
    @Override
    public void changeOrderAddress(Integer userId,Integer aid,Integer changeId) {
       addressDao.changeOrderAddress(userId,aid,changeId);
    }



    @Override
    public Orders getAddressUser(Integer id) {

        Orders addressUser = addressDao.getAddressUser(id);
        return addressUser;
    }

    @Override
    public List<AddAddress> getAddAddress(Integer userId) {
        List<AddAddress> addAddress = addressDao.getAddAddress(userId);
        return addAddress;
    }

    @Override
    public void changeDefaultAddress(int userId, int addressId) {
        addressDao.changeDefaultAddress(userId, addressId);
    }


}
